package com.miracle9.game.service;

import com.miracle9.game.dao.PayLogServiceDao;
import com.miracle9.game.dao.PromoterServiceDao1;
import com.miracle9.game.dao.SystemConfigServiceDao;
import com.miracle9.game.dao.UserServiceDao;
import com.miracle9.game.dao.GameDataLogServiceDao;
import com.miracle9.game.entity.PayLog;
import com.miracle9.game.entity.SystemConfig;
import com.miracle9.game.util.ProcessObjectStream;
import com.miracle9.game.util.DateDifferenceProcess;
import com.wmcy.bi.collect.CenterBISender;
import com.wmcy.bi.collect.RechargeGain;
import com.wmcy.bi.collect.RunStatistics;
import com.wmcy.bi.collect.SaleSet;
import com.wmcy.bi.collect.SubordinateNumber;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("centerBIService")
public class CenterBIService
{
  private static final Logger logger = Logger.getLogger(CenterBIService.class);
  @Autowired
  private SystemConfigServiceDao systemConfigServiceDao;
  @Autowired
  private UserServiceDao userServiceDao;
  @Autowired
  private PromoterServiceDao1 promoterServiceDao1;
  @Autowired
  private PayLogServiceDao payLogServiceDao;
  @Autowired
  private GameDataLogServiceDao gameDataLogServiceDao;
  
  public void sendSaleSet()
  {
    SystemConfig systemConfig = this.systemConfigServiceDao.getSystemConfig();
    String payScale = String.valueOf(systemConfig.getPayScale());
    String promoterPayScale = String.valueOf(systemConfig.getPromoterPayScale());
    SaleSet saleSet = ProcessObjectStream.setSaleSet(ProcessObjectStream.ar, payScale, promoterPayScale);
    logger.info("serverId:" + saleSet.getServerId() + ",userRechargeRate:" + saleSet.getUserRechargeRate() + ",DirectProRechargeRate:" + saleSet.getDirectProRechargeRate());
    CenterBISender.getInstance().sendSaleSet(saleSet);
    logger.info("sendSaleSet end");
  }
  
  public void sendSubordinateNumber()
  {
    logger.info("sendSubordinateNumber start");
    int directUserCount = this.userServiceDao.getQuantityByPromoterId_0();
    int notDirectUserCount = this.userServiceDao.getQuantityByPromoterId();
    int directPromoterCount = this.promoterServiceDao1.getQuantityByParentId_0();
    int notDirectPromoterCount = this.promoterServiceDao1.getQuantityByParentId();
    SubordinateNumber subordinateNumber = ProcessObjectStream.setSubordinateNumber(
    		ProcessObjectStream.ar, directUserCount, notDirectUserCount, directPromoterCount, notDirectPromoterCount);
    logger.info("serverId:" + subordinateNumber.getServerId() + ",directUserCount:" + subordinateNumber.getDirectUser() + 
      ",noDirectUser:" + subordinateNumber.getNoDirectUser() + ",directPromoter:" + subordinateNumber.getDirectPromoter() + 
      ",noDirectPromoter:" + subordinateNumber.getNoDirectPromoter());
    CenterBISender.getInstance().sendSubordinateNumber(subordinateNumber);
    logger.info("sendSubordinateNumber end");
  }
  
  public void sendRechargeGains()
  {
    logger.info("sendRechargeGains start");
    String yesterday = DateDifferenceProcess.getDateYMD(System.currentTimeMillis() - 86400000L);
    List<PayLog> payLogs = this.payLogServiceDao.getPayLogsByDay(yesterday);
    List<RechargeGain> someRechargeGain = new ArrayList();
    for (PayLog payLog : payLogs)
    {
      int userId = payLog.getUserId();
      int type = payLog.getType();
      int userType = payLog.getUserType();
      int operation = processType(type, userType);
      int currency = (int)payLog.getMoney();
      RechargeGain rechargeGain = ProcessObjectStream.setRechargeGain(ProcessObjectStream.ar, userId, operation, currency);
      someRechargeGain.add(rechargeGain);
    }
    for (RechargeGain rg : someRechargeGain) {
      logger.info("userId:" + rg.getUserId() + ",operation:" + rg.getOperation() + ",currency:" + rg.getCurrency() + ",serverId:" + rg.getServerId());
    }
    CenterBISender.getInstance().sendSomeRechargeGain(someRechargeGain);
    logger.info("sendRechargeGains end");
  }
  
  public void sendRunStatitistics()
  {
    logger.info("sendRunStatistics begin");
    String hql = "select type,sum(sum_ya_gold),sum(sum_de_gold) from game_data_log where datetime like ? group by datetime desc";
    String yesterday = DateDifferenceProcess.getDateYMD(System.currentTimeMillis() - 86400000L);
    List<Object[]> logs = this.gameDataLogServiceDao.getListByDateAndType(hql, yesterday, -1);
    List<RunStatistics> someRunStatistics = new ArrayList();
    for (Object[] o : logs)
    {
      int gameType = ((Integer)o[0]).intValue();
      int totalWan = ((Number)o[1]).intValue();
      int totalDe = ((Number)o[2]).intValue();
      int totalGain = totalWan - totalDe;
      long dateTime = DateDifferenceProcess.staticDateTime(yesterday);
      RunStatistics runStatistics = ProcessObjectStream.setRunStatistics(ProcessObjectStream.ar, totalWan, totalDe, totalGain, gameType, dateTime);
      someRunStatistics.add(runStatistics);
    }
    for (RunStatistics rs : someRunStatistics) {
      logger.info("gameType:" + rs.getGameType() + ",totalWan:" + rs.getTotalWan() + ",totalDe:" + rs.getTotalDe() + 
        ",totalGain:" + rs.getTotalGain() + ",date:" + rs.getDate());
    }
    CenterBISender.getInstance().sendSomeRunStatistics(someRunStatistics);
    logger.info("sendRunStatistics end");
  }
  
  public void sendUserOnLineDuration() {}
  
  private int processType(int type, int userType)
  {
    if ((type == 0) && (userType == 0)) {
      return 1;
    }
    if ((type == 0) && (userType == 1)) {
      return 2;
    }
    if ((type == 1) && (userType == 0)) {
      return 3;
    }
    if ((type == 1) && (userType == 1)) {
      return 4;
    }
    return 0;
  }
}
